package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"hotkid/appconfig"
	"log"
)

var DB *gorm.DB
var err error

func Init() {
	// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
	data := appconfig.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.Username, data.Password, data.Host, data.Port, data.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	log.Println("mysql 连接成功")
	err = DB.AutoMigrate(&User{})
	if err != nil {
		panic(err)
	}
	log.Println("mysql 迁移成功")
}
